package org.example.mine.config.rabbitmq;

import cn.hutool.core.util.ObjectUtil;
import com.rabbitmq.client.Channel;
import org.example.mine.service.TopUpService;
import org.example.model.domain.mine.ExpressRechargeRecord;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderReceiver {

    @Autowired
    private TopUpService topupService;
    // 监听消息队列

    @RabbitListener(queues = "user.order.receive_queue")
    public void consumeMessage(ExpressRechargeRecord order, Message message, Channel channel) throws Exception {
        System.err.println("接收到消息：" + order);
        if(ObjectUtil.isNotEmpty(order)){
            ExpressRechargeRecord expressRechargeRecord=topupService.selectorder(order.getTransactionNumber());
            if(ObjectUtil.isNotEmpty(expressRechargeRecord)){
                if(ObjectUtil.isNotEmpty(expressRechargeRecord.getStatus())){
                    if(expressRechargeRecord.getStatus()==2){
                        topupService.topupfila(expressRechargeRecord.getId());
                    }else{
                        System.out.println("订单已处理");
                    }
                }
            }
        }
//        因为在application.yml中配置了手动确认机制 所以在这里要使用Ack确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
